﻿using System.Globalization;
using System.Windows;
using GraphShape.Algorithms.Layout;
using GraphShape.Controls.Converters;
using GraphShape.Controls.Extensions;
using LiteDB;
using Point = GraphShape.Point;
using Size = GraphShape.Size;
// ReSharper disable JoinDeclarationAndInitializer

namespace KinonekoSoftware.Studio.Resources.Converters
{
    public abstract class EdgeGenerator : EdgeConverterBase
    {
        protected static void ExtractInputs(
            IList<object> values,
            out Point sourcePos,
            out Point targetPos,
            out Size sourceSize,
            out Size targetSize,
            out Avalonia.Point[] routeInformation)
        {
            // Get the position of the source
            sourcePos = new Point(
                                  values[0] != AvaloniaProperty.UnsetValue ? (double)values[0] : 0.0,
                                  values[1] != AvaloniaProperty.UnsetValue ? (double)values[1] : 0.0);

            // Get the size of the source
            sourceSize = new Size(
                                  values[2] != AvaloniaProperty.UnsetValue ? (double)values[2] : 0.0,
                                  values[3] != AvaloniaProperty.UnsetValue ? (double)values[3] : 0.0);

            // Get the position of the target
            targetPos = new Point(
                                  values[4] != AvaloniaProperty.UnsetValue ? (double)values[4] : 0.0,
                                  values[5] != AvaloniaProperty.UnsetValue ? (double)values[5] : 0.0);

            // Get the size of the target
            targetSize = new Size(
                                  values[6] != AvaloniaProperty.UnsetValue ? (double)values[6] : 0.0,
                                  values[7] != AvaloniaProperty.UnsetValue ? (double)values[7] : 0.0);

            // Get the route information
            routeInformation = values[8] != AvaloniaProperty.UnsetValue ? (Avalonia.Point[])values[8] : null;
        }

        protected static void ExtractValues(
            IList<object> values,
            out int sourceX,
            out int sourceY,
            out int targetX,
            out int targetY,
            out int offsetY)
        {
            double posX;
            double posY;
            double sizeY;

            posX = values[0] != AvaloniaProperty.UnsetValue ? (double)values[0] : 0.0;
            posY = values[1] != AvaloniaProperty.UnsetValue ? (double)values[1] : 0.0;

            sourceX = (int)posX;
            sourceY = (int)posY;

            posX  = values[4] != AvaloniaProperty.UnsetValue ? (double)values[4] : 0.0;
            posY  = values[5] != AvaloniaProperty.UnsetValue ? (double)values[5] : 0.0;
            sizeY = values[7] != AvaloniaProperty.UnsetValue ? (double)values[7] : 0.0;

            targetX = (int)posX;
            targetY = (int)posY;

            offsetY = (int)(sizeY / 3d);
        }
    }
}